package com.fau.mm.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.ObjectUtils;
import org.hibernate.Hibernate;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fau.mm.entity.MedicalAdmin;
import com.fau.mm.entity.MedicalResource;
import com.fau.mm.entity.MedicalRole;
import com.fau.mm.service.IUserManagerService;
import com.fau.mm.util.SystemConfig;


@Service
@Transactional(readOnly = true)
public class UserManagerService extends BaseService implements IUserManagerService {
	
	protected final MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();
	private Md5PasswordEncoder encoder;

	@SuppressWarnings("deprecation")
	@Override
	public MedicalAdmin findUserByName(String username) {
		MedicalAdmin admin = (MedicalAdmin) iBaseDao.getObjectByHql("from MedicalAdmin a where a.username = ?", username);
		if (ObjectUtils.equals(null, admin)) 
			throw new UsernameNotFoundException(messages.getMessage("JdbcDaoImpl.notFound", new Object[]{username}, "Username {0} not found"), username);
			Hibernate.initialize(admin.getRoleSet());
		if(0 == admin.getRoleSet().size())
			throw new UsernameNotFoundException(messages.getMessage("JdbcDaoImpl.noAuthority", new Object[] {username}, "User {0} has no GrantedAuthority"), username);
		return admin;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalResource> findAllResource() {
		List<MedicalResource> list = iBaseDao.getBySql("from MedicalResource a where a.typeName = ?", null, SystemConfig.MM_SYSTEM_TYPE);
		for(MedicalResource mr : list) {
			Hibernate.initialize(mr.getRoleSet());
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalResource> findRootResource() {
		List<MedicalResource> list = this.iBaseDao.getBySql("from MedicalResource a where a.isMenus=? ", null, SystemConfig.ROOT_RESOURCE_TYPE);
		for(MedicalResource mr : list) {
			Hibernate.initialize(mr.getRoleSet());
		}
		return list;
	}
	
	public List<MedicalResource> loadMenu() {
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		List<MedicalResource> list = new ArrayList<MedicalResource>();
		for(MedicalRole mr : user.getRoleSet()) {
			MedicalRole role = (MedicalRole) iBaseDao.getObject(MedicalRole.class, mr.getId());
			for(MedicalResource resource : role.getResourceSet()) {
				if(SystemConfig.IS_MENUS == resource.getIsMenus() && SystemConfig.MM_SYSTEM_TYPE.equals(resource.getTypeName())){
					list.add(resource);
				}
			}
		}
		for(int i = 0; i < list.size(); i++) {
			for(int j = list.size() - 1; j > i; j--) {
				if(list.get(i).getId().equals(list.get(j).getId())){
					list.remove(j);
				}
			}
		}
		return list;
	}
	
	public Md5PasswordEncoder getEncoder() {
		return encoder;
	}

	@Resource
	public void setEncoder(Md5PasswordEncoder encoder) {
		this.encoder = encoder;
	}


	
}
